package org.apaydin.beyazperde;

import org.apaydin.BeyazPerdeException;
import org.htmlcleaner.XPatherException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BeyazperdeWeeklyFilms {

    private final BeyazperdeWeeklyFilmsFetcher fetcher;

    private final Week week;

    private List<Film> films = new ArrayList<Film>();

    public BeyazperdeWeeklyFilms(Week week) {
        this.week = week;

        fetcher = new BeyazperdeWeeklyFilmsFetcherImpl(week);
    }

    public Film get(int index){
        return getFilms().get(index);
    }

    public List<Film> getFilms() {
        if(!fetched){
            throw new BeyazPerdeException("Filmler yüklenmedi. lütfen fetch metodunu çalıştır.");
        }
        return films;
    }

    public Week getWeek() {
        return week;
    }

    void addFilm(Film film) {
        films.add(film);
    }

    private boolean fetched = false;
    public void fetch() {
        try {
            films.clear();
            films.addAll(fetcher.fetch());
            fetched = true;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (XPatherException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

}
